import datastructure.linklist.ListNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/4 19:33
 * @description:
 */
public class Solution141 {
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != slow) {
            slow = slow.next;
            if (fast.next != null) {
                fast = fast.next;
            }
            if (fast.next != null) {
                fast = fast.next;
            }
        }
        return fast.next != null;
    }

    public static void main(String[] args) {
        ListNode n1 = new ListNode(3);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(0);
        ListNode n4 = new ListNode(-4);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n2;
        Solution141 solution = new Solution141();
        System.out.println(solution.hasCycle(n1));
    }
}
